<include file="public@header"/>

<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li class="active"><a href="{:url('Financial/index')}">财务收入列表</a></li>
    </ul>
    <form class="well form-inline margin-top-20 row" method="post" action="{:url('Financial/index')}">
        <label class="form-group col-lg-5 col-md-6 col-xs-9" style="margin-bottom:15px">
            学校-班级：
            <select class="form-control" name="school_id" id="school_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="schools">
                    <volist name="schools" id="vo">
                        <if condition="isset($param.school_id) && $param.school_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select> -
            <select class="form-control" name="class_id" id="class_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="classes">
                    <volist name="classes" id="vo">
                        <if condition="isset($param.class_id) && $param.class_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group col-lg-4 col-md-3 col-xs-3" style="margin-bottom:15px">
            课程：
            <select class="form-control" name="course_id" id="course_id" >
                <option value='0'>全部</option>
                <notempty name="courses">
                    <volist name="courses" id="vo">
                        <if condition="isset($param.course_id) && $param.course_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.course_name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.course_name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group col-lg-3 col-md-3 col-xs-3">支付状态：
            <select class="form-control" name="order_status" id="order_status">
                <option value='-1'>全部</option>
                <if condition="isset($param.order_status) && $param.order_status eq 0">
                    <option value="0" selected>待支付</option>
                    <else/>
                    <option value="0">待支付</option>
                </if>
                <if condition="isset($param.order_status) && $param.order_status eq 1">
                    <option value="1" selected>已支付</option>
                    <else/>
                    <option value="1">已支付</option>
                </if>
                <if condition="isset($param.order_status) && $param.order_status eq 2">
                    <option value="2" selected>已完成</option>
                    <else/>
                    <option value="2">已完成</option>
                </if>
                <if condition="isset($param.order_status) && $param.order_status eq 3">
                    <option value="3" selected>已取消</option>
                    <else/>
                    <option value="3">已取消</option>
                </if>
                <if condition="isset($param.order_status) && $param.order_status eq 4">
                    <option value="4" selected>已退课</option>
                    <else/>
                    <option value="4">已退课</option>
                </if>
            </select>
        </label>

        <label class="form-group col-lg-5 col-md-6 col-xs-9">
            缴费时间：&nbsp;&nbsp;
            <input type="text" class="form-control" name="start" id="start_pay" value="{$param.start|default=''}"
                   autocomplete="off"> -
            <input type="text" class="form-control" name="end" id="end_pay" value="{$param.end|default=''}"
                   autocomplete="off">
        </label>

        <label class="form-group col-lg-4 col-md-3 col-xs-3" >
            缴费方式：
            <select class="form-control" name="payment" id="pay_way">
                <option value='-1'>全部</option>
                <if condition="isset($param.payment) && $param.payment eq 0">
                    <option value="0" selected>微信</option>
                    <else/>
                    <option value="0">微信</option>
                </if>
                <if condition="isset($param.payment) && $param.payment eq 1">
                    <option value="1" selected>支付宝</option>
                    <else/>
                    <option value="1">支付宝</option>
                </if>
                <if condition="isset($param.payment) && $param.payment eq 2">
                    <option value="2" selected>现金</option>
                    <else/>
                    <option value="2">现金</option>
                </if>
                <if condition="isset($param.payment) && $param.payment eq 3">
                    <option value="3" selected>银行卡</option>
                    <else/>
                    <option value="3">银行卡</option>
                </if>
                <if condition="isset($param.payment) && $param.payment eq 4">
                    <option value="4" selected>代金券</option>
                    <else/>
                    <option value="4">代金券</option>
                </if>
                <if condition="isset($param.payment) && $param.payment eq 5">
                    <option value="5" selected>其他</option>
                    <else/>
                    <option value="5">其他</option>
                </if>
            </select>
        </label>

        <label class="form-group col-lg-3 col-md-3 col-xs-3" >
            发票：
            <select class="form-control" name="bill_status" id="bill_status">
                <option value='-1'>全部</option>
                <if condition="isset($param.bill_status) && $param.bill_status eq 0">
                    <option value="0" selected>不需要</option>
                    <else/>
                    <option value="0">不需要</option>
                </if>
                <if condition="isset($param.bill_status) && $param.bill_status eq 1">
                    <option value="1" selected>未开</option>
                    <else/>
                    <option value="1">未开</option>
                </if>
                <if condition="isset($param.bill_status) && $param.bill_status eq 2">
                    <option value="2" selected>已开</option>
                    <else/>
                    <option value="2">已开</option>
                </if>
            </select>
        </label>

        <label class="form-group col-md-offset-8 col-md-4" style="margin-top: 15px;">
            <input type="submit" class="btn btn-primary" value="搜索"/>
            <a class="btn btn-danger" href="{:url('Financial/index')}">清空</a>
        </label>
    </form>

    <form class=" col-md-2 form-group" style="float:right; margin-top:-77px; margin-right:50px;"
          method="post" action="{:url('Financial/exportExcel')}">
        <label class="form-group ">
            <input type="hidden" name="school" id="export_school" value="0">
            <input type="hidden" name="class" id="export_class" value="0">
            <input type="hidden" name="course" id="export_course" value="0">
            <input type="hidden" name="way" id="export_payment" value="-1">
            <input type="hidden" name="bstatus" id="export_bstatus" value="-1">
            <input type="hidden" name="ostatus" id="export_status" value="-1">
            <input type="hidden" name="start_time" id="export_start" value="0">
            <input type="hidden" name="end_time" id="export_end" value="0">

            <button type="submit" class="btn btn-primary" onclick="exportExcel()" title="根据筛选条件导出表格">导出</button>
            {$arr_error|default=''}
        </label>
    </form>

    <form class="js-ajax-form" action="" method="post">
        <table class="table table-hover table-bordered table-list">
            <thead>
            <tr>
                <th>编号</th>
                <th>学生</th>
                <th>家长</th>
                <th>家长手机号</th>
                <th>学校</th>
                <th>班级</th>
                <th>课程</th>
                <th>优惠金额(元)</th>
                <th>缴费方式</th>
                <th>实付金额(元)</th>
                <th>支付状态</th>
                <th>缴费时间</th>
                <th>发票</th>
                <th>经办人</th>
                <th>操作</th>
            </tr>
            </thead>
            <notempty name="order">
                <foreach name="order" item="vo">
                    <tr>
                        <td>{$vo.order_id}</td>
                        <td>{$vo.tname}</td>
                        <td>{$vo.realname}</td>
                        <td>{$vo.mobile}</td>
                        <td>{$vo.school_name}</td>
                        <td>{$vo.class_name}</td>
                        <td>{$vo.course_name}</td>
                        <td>{$vo.discount_amout}</td>
                        <if condition="$vo.payment eq 0">
                            <td>微信</td>
                            <elseif condition="$vo.payment eq 1"/>
                            <td>支付宝</td>
                            <elseif condition="$vo.payment eq 2"/>
                            <td>现金</td>
                            <elseif condition="$vo.payment eq 3"/>
                            <td>银行卡</td>
                            <elseif condition="$vo.payment eq 4"/>
                            <td>代金券</td>
                            <elseif condition="$vo.payment eq 5"/>
                            <td>其他</td>
                            <else/>
                            <td></td>
                        </if>
                        <td>{$vo.pay_amount}</td>
                        <if condition="$vo.order_status eq 0">
                            <td><span class="label label-warning">待支付</span></td>
                        <elseif condition="$vo.order_status eq 1"/>
                            <td><span class="label label-success">已支付</span></td>
                        <elseif condition="$vo.order_status eq 2"/>
                            <td><span class="label label-info">已完成</span></td>
                        <elseif condition="$vo.order_status eq 3"/>
                            <td><span class="label label-default">已取消</span></td>
                        <elseif condition="$vo.order_status eq 4"/>
                            <td><span class="label label-danger">已退课</span></td>
                        </if>
                        <if condition="isset($vo.pay_time) && $vo.pay_time gt 0">
                            <td>{$vo.pay_time|date="Y-m-d H:i",###}</td>
                        <else/>
                            <td></td>
                        </if>

                        <if condition="$vo.bill_status eq 2">
                            <td>
                                <a class="view_bill" attr_id="{$vo.id}" title="查看发票" style="cursor:pointer;" >
                                    已开
                                </a>
                            </td>
                        <elseif condition="$vo.bill_status eq 1"/>
                            <td>
                                <a class="view_bill" attr_id="{$vo.id}" title="查看发票" style="cursor:pointer;">
                                    未开
                                </a>
                            </td>
                        <else/>
                            <td>不需要</td>
                        </if>
                        <td>{$vo.operator|default=''}</td>
                        <td>
                            <if condition="$vo.order_status eq 0">
                                <a href="javascript:;" class="pay all_id" attr_id="{$vo.id}">缴费</a>&nbsp;|
                                <a href="javascript:;" id="discount_money" attr_id="{$vo.id}" data-amount="{$vo.pay_amount}">修改价格</a>&nbsp;|
                                <a href="{:url('Financial/cancel',array('id'=>$vo.id))}" class="js-ajax-delete" data-msg="确定要取消订单吗？" attr_id="{$vo.id}">取消订单</a>
                            </if>
                            <if condition="$vo.order_status eq 1 && $vo.bill_status eq 1">
                                <a href="{:url('Financial/handle_bill',array('id'=>$vo.id))}" data-msg="确定要开发票吗？" class="js-ajax-delete">开发票</a>&nbsp;
                            </if>
                            <!--<a href="#" class="disabled">打印</a>-->
                        </td>
                    </tr>
                </foreach>
            </notempty>
        </table>
        <div class="pagination">{$page|default=''}</div>
    </form>
</div>
<div id="stu_change_class" style="display: none;">
    <form action="{:url('Financial/pay')} " class="well  row js-ajax-form margin-top-20" method="post" style="margin:0;">
        <input type="hidden" id="pay_id" name="id" value="">

        <label class="form-group">付款方式:
            <select class="form-control" name="payment" id="payment" style="width:179px;">
                <option value='0'>微信</option>
                <option value='1'>支付宝</option>
                <option value='2'>现金</option>
                <option value='3'>银行卡</option>
                <option value='4'>代金券</option>
                <option value='5'>其他</option>
            </select>
        </label>
        <label class="form-group">备注:
            <textarea class="form-control" name="remark" rows="6" style="width:280px;"></textarea>
        </label>

        <button type="submit"  id="stu_change_submit" class='js-ajax-submit' style="display:none"></button>
    </form>
</div>

<div id="edit_order_price" style="display: none;">
    <form action="{:url('Financial/setOrderPrice')}" class="well row js-ajax-form margin-top-20" method="post" style="margin:0;">
        <input type="hidden" id="orderId" name="id" value="">

        <label class="form-group">实付金额(元)：
            <input type="text" class="form-control" min="0" name="real_pay" id="real_pay" disabled value=""/>
        </label>
        <label class="form-group">立减金额(元)：
            <input type="number" class="form-control" min="0" name="discount" value=""/>
        </label>
        <div><span class="glyphicon glyphicon-info-sign"></span> <i class="info" style="color: red;">立减金额设为 0元，就可以还原实付金额</i></div>

        <button type="submit" id="edit_order_submit" class="js-ajax-submit" style="display:none"></button>
    </form>
</div>

<div id="view_bill" style="display:none;padding:15px;">
    <table class="table table-bordered table-list table-hover">
        <thead>
            <tr>
                <th>发票ID</th>
                <th>发票类型</th>
                <th>公司名称</th>
                <th>公司税号</th>
                <th>获取方式</th>
                <th>收件人姓名</th>
                <th>联系方式</th>
                <th>地址</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

<script src="__STATIC__/js/admin.js"></script>

<script>

    $(document).ready(function () {
        $('#school_id').on('change',function () {
            var sid = $(this).val();
            if(sid == 0){
                $('#class_id').empty();
                var html = '<option value=\"0\">全部</option>';
                $('#class_id').html(html);
                return false;
            }
            $.ajax({
                url:"{:url('Financial/ajax_class')}",
                type:'post',
                dataType:'json',
                data:{"school_id":sid},
                success:function(jsonData){
                    $('#class_id').empty();
                    var html = '<option value=\"0\">全部</option>';

                    if(jsonData.code == 1){
                        for(var i in jsonData.data){
                            html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                        }
                    }
                    $('#class_id').html(html);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
                }
            });
        });
        $('input[name="start"]').datepicker();
        $('input[name="end"]').datepicker();
    });

    //导出
    function exportExcel() {
        var schoolId = $('#school_id').val();
        var classId = $('#class_id').val();
        var courseId = $('#course_id').val();
        var payment = $('#pay_way').val();
        var btstatus = $('#bill_status').val();
        var status = $('#order_status').val();
        var start = $('#start_pay').val();
        var end = $('#end_pay').val();

        $('#export_school').val(schoolId);
        $('#export_class').val(classId);
        $('#export_course').val(courseId);
        $('#export_payment').val(payment);
        $('#export_bstatus').val(btstatus);
        $('#export_status').val(status);
        $('#export_start').val(start);
        $('#export_end').val(end);
    }

    //修改订单金额
    $('body').on('click','#discount_money',function () {
        var order_id = $(this).attr('attr_id');
        var amount = $(this).data('amount');
        $("#orderId").val(order_id);
        $("#real_pay").val(amount);

        layer.open({
            type: 1,
            btn: ['提交'],
            title: '修改订单价格',
            content: $('#edit_order_price'),
            yes: function (index, layero) {
                $('#edit_order_submit').click();
            }
        })
    });

    //手动支付
    $('body').on('click','.pay',function () {
        orginal_cid=$(this).attr('attr_id');
        $("#pay_id").val(orginal_cid);
        layer.open({
            type: 1,
            btn: ['提交'],
            title: '支付方式',
            content: $('#stu_change_class'),
            yes: function (index, layero) {
                $('#stu_change_submit').click();
            }
        })
    });

    $('body').on('click','.view_bill',function () {
        var order_id=$(this).attr('attr_id');
        $.ajax({
            url:"{:url('Financial/view_bill')}",
            type:'get',
            dataType:'json',
            data:{"order_id":order_id},
            success:function(jsonData){
                console.log(jsonData);
                $('#view_bill>table>tbody>tr>td:eq(0)').text(jsonData[0].id);
                //个人发票还是公司
                if(jsonData[0]['bill_type']==1){
                    $('#view_bill>table>tbody>tr>td:eq(1)').text('个人');
                    $('#view_bill>table>tbody>tr>td:eq(2)').text('-');
                    $('#view_bill>table>tbody>tr>td:eq(3)').text('-');
                }else if(jsonData[0]['bill_type']==2){
                    $('#view_bill>table>tbody>tr>td:eq(1)').text('公司');
                    $('#view_bill>table>tbody>tr>td:eq(2)').text(jsonData[0].payee);
                    $('#view_bill>table>tbody>tr>td:eq(3)').text(jsonData[0]['tax_code']);
                }
                //邮寄还是自取
                if(jsonData[0]['obtain_type']==1){
                    $('#view_bill>table>tbody>tr>td:eq(4)').text('快递到付');
                    $('#view_bill>table>tbody>tr>td:eq(5)').text(jsonData[0].recipient);
                    $('#view_bill>table>tbody>tr>td:eq(6)').text(jsonData[0].phone);
                    $('#view_bill>table>tbody>tr>td:eq(7)').text(jsonData[0].address);
                }else if(jsonData[0]['obtain_type']==2){
                    $('#view_bill>table>tbody>tr>td:eq(4)').text('教学点自取');
                    $('#view_bill>table>tbody>tr>td:eq(5)').text('-');
                    $('#view_bill>table>tbody>tr>td:eq(6)').text('-');
                    $('#view_bill>table>tbody>tr>td:eq(7)').text('-');
                }
                layer.open({
                    type: 1,
                    //btn: ['确定'],
                    area:['800px','auto'],
                    title: '查看发票信息',
                    content: $('#view_bill'),
                })
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });

    });


</script>
</body>